﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace IESN.MyControl
{
    [ToolboxBitmap(@"e:\dms_ico\TRFFC09.ICO")]
    public partial class ImgMutilStatusButton : Control, Ibutton
    {
        private Image _ImagesMid;
        private Image _ImagesOff;
        private Image _ImagesOn;
        private short _itemIndex;
        private short _numStatus;
        private IContainer components;
        private bool mEnable;
        private float msngBorderWidth;
        private AlarmStatus mStatus;
        private float mTextFontSize;
        private string mtextMidString;
        private string mtextOffString;
        private string mtextOnString;
        private string mtextString;
        private Brush mTextStringForeColor;
        private Color old_BackColor;
        public AlarmStatus Status_old;

        public event StatusChangedEventHandler StatusChangedEvent;

        public ImgMutilStatusButton()
        {
            base.Resize += new EventHandler(this.Alarm_Resize);
            base.Layout += new LayoutEventHandler(this.Alarm_Layout);
            base.MouseUp += new MouseEventHandler(this.mybutton_MouseUp);
            this.mStatus = AlarmStatus.StatusOff;
            this.Status_old = AlarmStatus.StatusOff;
            this.msngBorderWidth = 1.5f;
            this.mtextOnString = "On";
            this.mtextOffString = "Off";
            this.mtextMidString = "Mid";
            this.mtextString = "";
            this.mTextFontSize = 10f;
            this.mEnable = true;
            this._itemIndex = 0;
            this.InitializeComponent();
            this.old_BackColor = this.BackColor;
        }

        public ImgMutilStatusButton(Image ImgOn, Image ImgMid, Image ImgOFF)
        {
            base.Resize += new EventHandler(this.Alarm_Resize);
            base.Layout += new LayoutEventHandler(this.Alarm_Layout);
            base.MouseUp += new MouseEventHandler(this.mybutton_MouseUp);
            this.mStatus = AlarmStatus.StatusOff;
            this.Status_old = AlarmStatus.StatusOff;
            this.msngBorderWidth = 1.5f;
            this.mtextOnString = "On";
            this.mtextOffString = "Off";
            this.mtextMidString = "Mid";
            this.mtextString = "";
            this.mTextFontSize = 10f;
            this.mEnable = true;
            this._itemIndex = 0;
            this.InitializeComponent();
            this.ImageOn = ImgOn;
            this.ImageOFF = ImgOFF;
            this.ImageMid = ImgMid;
            this.old_BackColor = this.BackColor;
        }

        private void Alarm_Layout(object sender, LayoutEventArgs e)
        {
            string affectedProperty = e.AffectedProperty;
            if (string.Equals(affectedProperty, "Bounds", StringComparison.CurrentCultureIgnoreCase))
            {
                this.Width = (int)Math.Round((double)(((double)(this.Height * 2)) / 3.0));
            }
            else
            {
                this.Width = (int)Math.Round((double)(((double)(this.Height * 2)) / 3.0));
            }
        }

        private void Alarm_Resize(object sender, EventArgs e)
        {
            this.Invalidate();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void DrawLight(AlarmStatus ButtonStatus, Graphics grfGraphics)
        {
            PointF tf;
            int x = (int)Math.Round((double)(this.Size.Width * 0.01));
            int y = (int)Math.Round((double)(((double)(this.Size.Height * 1)) / 3.5));
            int width = (int)Math.Round((double)(this.Size.Width * 0.98));
            Brush brush = new SolidBrush(Color.FromArgb(0xff, 0x80, 0x80, 0xff));
            if (ButtonStatus == AlarmStatus.StatusOff)
            {
                x = (int)Math.Round((double)(this.Size.Width * 0.01));
                y = (int)Math.Round((double)(((double)(this.Size.Height * 1)) / 3.0));
                width = (int)Math.Round((double)(this.Size.Width * 0.98));
                Rectangle rect = new Rectangle(x, y, width, width);
                if (ImageOFF != null)
                {
                    grfGraphics.DrawImage(this.ImageOFF, rect);
                }
                Font font = new Font("Times New Roman", this.TextFontSize, FontStyle.Regular);
                Brush red = Brushes.Red;
                SizeF ef = grfGraphics.MeasureString(this.TextOffString, font);
                tf = new PointF((float)Convert.ToInt16((float)(this.Width - ef.Width)), Convert.ToSingle((double)(this.Size.Height * 0.02)));
                grfGraphics.DrawString(this.TextOffString, font, red, tf);
                this.BackColor = this.old_BackColor;
            }
            else if (ButtonStatus == AlarmStatus.StatusOn)
            {
                x = (int)Math.Round((double)(this.Size.Width * 0.01));
                y = (int)Math.Round((double)(((double)(this.Size.Height * 1)) / 3.0));
                width = (int)Math.Round((double)(this.Size.Width * 0.98));
                Rectangle rectangle2 = new Rectangle(x, y, width, width);
                grfGraphics.DrawImage(this.ImageOn, rectangle2);
                Font font2 = new Font("Times New Roman", this.TextFontSize, FontStyle.Regular);
                Brush green = Brushes.Green;
                SizeF ef2 = grfGraphics.MeasureString(this.TextOnString, font2);
                tf = new PointF(Convert.ToSingle((double)(this.Size.Width * 0.02)), Convert.ToSingle((double)(this.Size.Height * 0.02)));
                grfGraphics.DrawString(this.TextOnString, font2, green, tf);
                this.BackColor = Color.Chartreuse;
            }
            else
            {
                x = (int)Math.Round((double)(this.Size.Width * 0.01));
                y = (int)Math.Round((double)(((double)(this.Size.Height * 1)) / 3.0));
                width = (int)Math.Round((double)(this.Size.Width * 0.98));
                Rectangle rectangle3 = new Rectangle(x, y, width, width);
                grfGraphics.DrawImage(this.ImageMid, rectangle3);
                Font font3 = new Font("Times New Roman", this.TextFontSize, FontStyle.Regular);
                Brush darkCyan = Brushes.DarkCyan;
                SizeF ef3 = grfGraphics.MeasureString(this.TextMidString, font3);
                tf = new PointF((float)Convert.ToInt16((double)((((double)this.Width) / 2.0) - (ef3.Width / 2f))), Convert.ToSingle((double)(this.Size.Height * 0.02)));
                grfGraphics.DrawString(this.TextMidString, font3, darkCyan, tf);
                this.BackColor = this.old_BackColor;
            }
            brush.Dispose();
        }

        public void EnableObject(bool blnEnable)
        {
            this.enable = blnEnable;
        }

        // [DebuggerStepThrough]
        private void InitializeComponent()
        {
            this.Name = "UserControl1";
            Size size = new Size(0x128, 0x128);
            this.Size = size;
        }

        private void mybutton_MouseUp(object sender, MouseEventArgs e)
        {
            if (this.mEnable)
            {
                if (e.X < (((double)this.Size.Width) / 3.0))
                {
                    this.Status = AlarmStatus.StatusOn;
                }
                else if (e.X < (((double)(this.Size.Width * 2)) / 3.0))
                {
                    this.Status = AlarmStatus.StatusMid;
                }
                else
                {
                    this.Status = AlarmStatus.StatusOff;
                }
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            Rectangle rectangle = new Rectangle();
            base.OnPaint(pe);
            Graphics grfGraphics = pe.Graphics;
            Pen pen = new Pen(Color.White, this.msngBorderWidth);
            rectangle.X = 1;
            rectangle.Y = 1;
            rectangle.Height = this.Height;
            rectangle.Width = this.Width;
            grfGraphics.DrawRectangle(pen, rectangle);
            pen = new Pen(Color.Black, this.msngBorderWidth);
            grfGraphics.DrawLine(pen, 1, this.Height - 1, this.Width - 1, this.Height - 1);
            grfGraphics.DrawLine(pen, this.Width - 1, 1, this.Width - 1, this.Height - 1);
            Font font = new Font("Times New Roman", this.TextFontSize, FontStyle.Regular);
            Brush black = Brushes.Black;
            SizeF ef = grfGraphics.MeasureString(this.TextOnString, font);
            PointF point = new PointF(Convert.ToSingle((double)(this.Size.Width * 0.02)), Convert.ToSingle((double)(this.Size.Height * 0.02)));
            grfGraphics.DrawString(this.TextOnString, font, black, point);
            black = Brushes.Black;
            ef = grfGraphics.MeasureString(this.TextOffString, font);
            point = new PointF((float)Convert.ToInt16((float)(this.Width - ef.Width)), Convert.ToSingle((double)(this.Size.Height * 0.02)));
            grfGraphics.DrawString(this.TextOffString, font, black, point);
            black = Brushes.Black;
            ef = grfGraphics.MeasureString(this.TextMidString, font);
            point = new PointF((float)Convert.ToInt16((double)((((double)this.Width) / 2.0) - (ef.Width / 2f))), Convert.ToSingle((double)(this.Size.Height * 0.02)));
            grfGraphics.DrawString(this.TextMidString, font, black, point);
            font = new Font("Times New Roman", this.TextFontSize, FontStyle.Bold);
            if (this.TextStringForeColor == null)
            {
                black = Brushes.Black;
            }
            else
            {
                black = this.TextStringForeColor;
            }
            ef = grfGraphics.MeasureString(this.TextString, font);
            point = new PointF((float)Convert.ToInt16((double)((((double)this.Size.Width) / 2.0) - (ef.Width / 2f))), (float)Convert.ToInt16((double)(((double)this.Height) / 6.0)));
            grfGraphics.DrawString(this.TextString, font, black, point);
            this.DrawLight(this.Status, grfGraphics);
            pen.Dispose();
            grfGraphics.Dispose();
        }

        public void ResetStatus()
        {
            this.Status = AlarmStatus.StatusOff;
        }

        public bool ShouldSerializeStatus()
        {
            if (this.mStatus == AlarmStatus.StatusOff)
            {
                return false;
            }
            return true;
        }

        [DefaultValue((float)1f), Description("周围边框的宽度")]
        public float BorderWidth
        {
            get
            {
                return this.msngBorderWidth;
            }
            set
            {
                if (this.msngBorderWidth != value)
                {
                    this.msngBorderWidth = value;
                    this.Invalidate();
                }
            }
        }

        public bool enable
        {
            get
            {
                return this.mEnable;
            }
            set
            {
                this.mEnable = value;
            }
        }

        public Image ImageMid
        {
            get
            {
                return this._ImagesMid;
            }
            set
            {
                this._ImagesMid = value;
            }
        }

        public Image ImageOFF
        {
            get
            {
                return this._ImagesOff;
            }
            set
            {
                this._ImagesOff = value;
            }
        }

        public Image ImageOn
        {
            get
            {
                return this._ImagesOn;
            }
            set
            {
                this._ImagesOn = value;
            }
        }

        public short ItemIndex
        {
            get
            {
                return this._itemIndex;
            }
            set
            {
                this._itemIndex = value;
            }
        }

        //public short myControl.Ibutton.numStatus
        //{
        //    get
        //    {
        //        return this._numStatus;
        //    }
        //    set
        //    {
        //        this._numStatus = value;
        //    }
        //}

        public short numStatus
        {
            get
            {
                return this._numStatus;
            }
            set
            {
                this._numStatus = value;
            }
        }

        [Description("状态")]
        public AlarmStatus Status
        {
            get
            {
                return this.mStatus;
            }
            set
            {
                if (this.mStatus != value)
                {
                    this.Status_old = this.mStatus;
                    this.mStatus = value;
                    if (this.StatusChangedEvent != null)
                    {
                        this.StatusChangedEvent(this, this.mStatus);
                    }
                    this.Invalidate();
                }
            }
        }

        public float TextFontSize
        {
            get
            {
                return this.mTextFontSize;
            }
            set
            {
                if (this.mTextFontSize != value)
                {
                    this.mTextFontSize = value;
                    this.Invalidate();
                }
            }
        }

        public string TextMidString
        {
            get
            {
                return this.mtextMidString;
            }
            set
            {
                if (string.Equals(this.mtextMidString, value, StringComparison.CurrentCultureIgnoreCase))
                {
                    this.mtextMidString = value;
                    this.Invalidate();
                }
            }
        }

        public string TextOffString
        {
            get
            {
                return this.mtextOffString;
            }
            set
            {
                if (string.Equals(this.mtextOffString, value, StringComparison.CurrentCultureIgnoreCase))
                {
                    this.mtextOffString = value;
                    this.Invalidate();
                }
            }
        }

        public string TextOnString
        {
            get
            {
                return this.mtextOnString;
            }
            set
            {
                if (string.Equals(this.mtextOnString, value, StringComparison.CurrentCultureIgnoreCase))
                {
                    this.mtextOnString = value;
                    this.Invalidate();
                }
            }
        }

        public string TextString
        {
            get
            {
                return this.mtextString;
            }
            set
            {
                if (string.Equals(this.mtextString, value, StringComparison.CurrentCultureIgnoreCase))
                {
                    this.mtextString = value;
                    this.Invalidate();
                }
            }
        }

        public Brush TextStringForeColor
        {
            get
            {
                return this.mTextStringForeColor;
            }
            set
            {
                if (!object.Equals(this.mTextStringForeColor, value))
                {
                    this.mTextStringForeColor = value;
                    this.Invalidate();
                }
            }
        }

        public enum AlarmStatus
        {
            StatusMid = 3,
            StatusOff = 2,
            StatusOn = 1
        }

        public delegate void StatusChangedEventHandler(object Sender, ImgMutilStatusButton.AlarmStatus NewStatus);
    }
}
